package id.co.rho.scoring.cid.client.helper;

/**
 * Created by IntelliJ IDEA.
 * User: frizky
 * Date: 9/15/11
 * Time: 10:38 AM
 */

import com.extjs.gxt.ui.client.GXT;
import com.extjs.gxt.ui.client.data.BaseFilterConfig;
import com.extjs.gxt.ui.client.data.BaseNumericFilterConfig;
import com.extjs.gxt.ui.client.data.FilterConfig;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.grid.filters.Filter;
import com.extjs.gxt.ui.client.widget.grid.filters.RangeMenu;

import java.util.ArrayList;
import java.util.List;

public class CustomNumericFilter extends Filter {
    private NumberField field;
    private String prefix;

    public static class NumericFilterMessages extends FilterMessages {
        private String emptyText = GXT.MESSAGES.numericFilter_emptyText();

        /**
         * Returns the field's empty text.
         *
         * @return the empty text
         */
        public String getEmptyText() {
            return emptyText;
        }

        /**
         * Sets the field empty text (defaults to 'Enter filter text...').
         *
         * @param emptyText the empty text
         */
        public void setEmptyText(String emptyText) {
            this.emptyText = emptyText;
        }
    }

//    private List<RangeItem> rangeItems = new ArrayList<RangeItem>();

    private RangeMenu rangeMenu;
    private int width = 125;

    public CustomNumericFilter(String dataIndex) {
        super(dataIndex);
        setMessages(new NumericFilterMessages());
//        rangeItems.add(RangeItem.LESSTHAN);
//        rangeItems.add(RangeItem.GREATERTHAN);
//        rangeItems.add(RangeItem.EQUAL);

//        menu = new RangeMenu(this);
//        rangeMenu = (RangeMenu) menu;
//        rangeMenu.setRangeItems(rangeItems);
        setWidth(getWidth());
    }

    public CustomNumericFilter(final String dataIndex, final NumberField field, final Button submitButton) {
        super(dataIndex);
        this.field = field;

        submitButton.addSelectionListener(new SelectionListener<ButtonEvent>() {
            @Override
            public void componentSelected(ButtonEvent ce) {
                fireUpdate();
            }
        });
        setWidth(getWidth());

        setMessages(new NumericFilterMessages());
    }

    public CustomNumericFilter(final String dataIndex, final NumberField field, final Button submitButton, String prefix) {
        super(dataIndex);
        this.field = field;
        this.prefix = prefix;

        submitButton.addSelectionListener(new SelectionListener<ButtonEvent>() {
            @Override
            public void componentSelected(ButtonEvent ce) {
                fireUpdate();
            }
        });
        setWidth(getWidth());

        setMessages(new NumericFilterMessages());
    }


    @Override
    public NumericFilterMessages getMessages() {
        return (NumericFilterMessages) super.getMessages();
    }

    @Override
    public List<FilterConfig> getSerialArgs() {
        List<FilterConfig> configs = new ArrayList<FilterConfig>();
        BaseFilterConfig c = new BaseNumericFilterConfig("numeric", "eq", getValue());
        c.setField(dataIndex);
        c.setValue(getValue());
        c.setComparison(" = ");
        // untuk field custom karena field BaseFilter akan di overide dengan nilai dataIndex oleh listener yang ada
        c.set("extField", prefix != null ? prefix.concat(dataIndex) : dataIndex);
        configs.add(c);
        return configs;
    }

    @Override
    public Object getValue() {
        return field.getValue();
    }

    /**
     * Returns the field width.
     *
     * @return the field width
     */
    public int getWidth() {
        return width;
    }

    @Override
    public boolean isActivatable() {
//        if (rangeMenu.eq != null && rangeMenu.eq.getValue() != null) {
//            return true;
//        }
//        if (rangeMenu.lt != null && rangeMenu.lt.getValue() != null) {
//            return true;
//        }
//        if (rangeMenu.gt != null && rangeMenu.gt.getValue() != null) {
//            return true;
//        }
        return field.getValue() != null;
    }

    @Override
    public void setMessages(FilterMessages messages) {
        super.setMessages(messages);
        if (this.field != null) {
            this.field.setEmptyText(getMessages().getEmptyText());
        }
    }

    @SuppressWarnings({"unchecked", "rawtypes"})
    @Override
    public void setValue(Object value) {
        this.field.setValue((Number) value);
        fireUpdate();
    }

    /**
     * Sets the field width (defaults to 125).
     *
     * @param width the field width
     */
    public void setWidth(int width) {
        this.width = width;
    }

    @Override
    public boolean validateModel(ModelData model) {
        Number val = getModelValue(model);
        if (val == null || this.field.getValue().doubleValue() != val.doubleValue()) {
            return false;
        }
//
//        if (rangeMenu.lt != null && rangeMenu.lt.getValue() != null) {
//            if (val == null || rangeMenu.lt.getValue().doubleValue() <= val.doubleValue()) {
//                return false;
//            }
//        }
//
//        if (rangeMenu.gt != null && rangeMenu.gt.getValue() != null) {
//            if (val == null || rangeMenu.gt.getValue().doubleValue() >= val.doubleValue()) {
//                return false;
//            }
//        }
        return true;
    }
}